//
//  KZOptionView.m
//  lg_zhinengbeikao_english
//
//  Created by 朱凯 on 2018/12/17.
//  Copyright © 2018年 zhukai. All rights reserved.
//

#import "KZOptionView.h"

@implementation KZOptionView
{
    CAGradientLayer *_bgLayer;
    UILabel *_label;
}

- (instancetype)init {
    if (self = [super init]) {
        
        self.userInteractionEnabled = false;
        
        _label = [[UILabel alloc] init];
        _label.font = B_FONT(18);
        _label.textAlignment = NSTextAlignmentCenter;
        _label.textColor = UIColor.whiteColor;
        [self addSubview:_label];
        [_label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
    }
    return self;
}
- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    if (!_bgLayer) {
        
        self.layer.cornerRadius = self.width / 2;
        self.layer.masksToBounds = true;
        
        _bgLayer = [CAGradientLayer layer];
        _bgLayer.bounds = CGRectMake(0, 0, self.width, self.height);
        _bgLayer.position = CGPointMake(self.width / 2, self.height / 2);
        _bgLayer.startPoint = CGPointMake(0.5, 0);
        _bgLayer.endPoint = CGPointMake(0.5, 1);
        [self.layer insertSublayer:_bgLayer atIndex:0];
    }
    
    NSArray *colors1 = @[rgba(240, 240, 240, 1),
                         rgba(81, 175, 243, 1),
                         rgba(110, 178, 56, 1),
                         rgba(218, 105, 102, 1),];
    NSArray *colors2 = @[rgba(200, 200, 200, 1),
                         rgba(50, 108, 233, 1),
                         rgba(82, 164, 53, 1),
                         rgba(198, 70, 65, 1),];
    
    UIColor *color1 = colors1[(int)_status];
    UIColor *color2 = colors2[(int)_status];
    _bgLayer.colors = @[(__bridge id)color1.CGColor,
                        (__bridge id)color2.CGColor,];
    
    if (_status == KZOptionStatus_none) {
        _label.textColor = UIColor.blackColor;
    } else {
        _label.textColor = UIColor.whiteColor;
    }
}
- (void)setStatus:(KZOptionStatus)status {
    
    _status = status;
    
    [self setNeedsLayout];
    [self layoutIfNeeded];
}
- (void)setText:(NSString *)text {
    _text = text;
    _label.text = text;
}
@end
